package com.example.swordjava;


public class Test22 {

    public static void main(String[] args) {

        int[] a = {2, 3, 1, 0, 2, 5, 3};
    }

    class ListNode {
        ListNode next = null;
        int val;

        public ListNode(int val) {
            this.val = val;
        }
    }

    /**
     * 链表中倒数最后k个结点
     */

    public ListNode FindKthToTail(ListNode pHead, int k) {
        // write code here
        ListNode pre = pHead;
        ListNode behind = pHead;
        int count = 0;
        while (pre != null) {
            if (count >= k) {
                behind = behind.next;
            }
            count++;
            pre = pre.next;
        }
        if (count < k) {
            return null;
        }
        return behind;
    }

}
